var app = getApp();
Page({
  data: {
    tabbar: {},
    pingLunData:{isShowPingLun:0,value:'',isshuru:0,yifa:0},
    isShowPingLun:1,
    pingLunContent:'',
    id:22,
    from:1,
    lunbotu:[],
    hotShuPing:[],
      hot:[],
      tuijian1:[],
      tuijian2:[],
      wenda:[],
      xiewen:[],
      keyword:'',

  },
  toinfo:function(res){
  	console.log('点击跳转')
  	wx.navigateTo({
  		url:'/pages/find/bookinfo'
  	})
  }, 
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    app.editTabBar();
    console.log(this.data.tabbar)
    this.getLunBoList();
  },

    onShow:function(e){
        this.getDataList()
    },

  hidePingLun:function(){
    this.setData({
      pingLunData:{isShowPingLun:0}
    })
  },


  showPingLun:function(e){
    this.setData({
      pingLunData:{isShowPingLun:1,yifa:0},
      pingLunContent:'',
      id:e.currentTarget.dataset.id,
      from:e.currentTarget.dataset.from
    })
  },

    onPullDownRefresh:function(e){
      // console.log(123)
        var that = this;
      wx.showLoading({
          title:'用生命在加载'
      });
        setTimeout(function(){
            that.getDataList()
        },1000)

    },

  moreShuPing:function(e){
    wx.navigateTo({
      url:'/pages/community/shuping/index'
    })
  },
  moreHuaTi:function(e){
    wx.navigateTo({
      url:'/pages/community/huati/index'
    })
  },
  moreWenDa:function(e){
    wx.navigateTo({
      url:'/pages/community/wenda/index'
    })
  },
  moreXieWen:function(e){
    wx.navigateTo({
      url:'/pages/community/xiewen/index'
    })
  },
  toShuPingInfo:function(e){
    var id = e.currentTarget.dataset.id;
    console.log('hot'+id)
    wx.navigateTo({
      url:'/pages/community/shuping/info?id='+id
    })
  }
  ,
  toHuaTiInfo:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url:'/pages/community/huati/info?id='+id
    })
  }
  ,
  toWenDaInfo:function(e){
      var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url:'/pages/community/wenda/info?id='+id
    })
  }
  ,
  toXieWenInfo:function(e){
      var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url:'/pages/community/xiewen/info?id='+id
    })
  },
  //点赞
  dianzan:function(e){
    var that = this;
    var from = e.currentTarget.dataset.from;
    var id = e.currentTarget.dataset.id;
    var formdata = {
      otherid:id,
      from:from
    }
    app.dianZan(formdata);
    //修改当前数据源，避免重新请求数据
    //3社区的书评 4社区的话题 5社区的问答 6社区的写文
    switch(parseInt(from)){
      case 3:
        var temp = that.data.hot;
        for(var i=0;i<temp.length;i++){
          var item = temp[i];
          if(id == item.id){
            if(1 == item.isDianZan){
              item.dianzan_num=--item.dianzan_num;
              item.isDianZan=0;
            }else if(0 == item.isDianZan){
              item.dianzan_num=++item.dianzan_num;
              item.isDianZan=1;
            }
          }
        }
        that.setData({
          hot:temp
        })
        // wx.showToast({
        //   title:'点赞成功',
        //   icon:'success',
        //   duration:2000
        // })
      break;

      case 4:
      var temp = that.data.hot;
        for(var i=0;i<temp.length;i++){
          var item = temp[i];
          if(id == item.id){
            if(1 == item.isDianZan){
              item.dianzan_num=--item.dianzan_num;
              item.isDianZan=0;
            }else if(0 == item.isDianZan){
              item.dianzan_num=++item.dianzan_num;
              item.isDianZan=1;
            }
          }
        }
        that.setData({
          hot:temp
        })
      break;
      case 5:
      var temp = that.data.wenda;
        for(var i=0;i<temp.length;i++){
          var item = temp[i];
          if(id == item.id){
            if(1 == item.isDianZan){
              item.dianzan_num=--item.dianzan_num;
              item.isDianZan=0;
            }else if(0 == item.isDianZan){
              item.dianzan_num=++item.dianzan_num;
              item.isDianZan=1;
            }
          }
        }
        that.setData({
          wenda:temp
        })
      break;
      case 6:
      var temp = that.data.xiewen;
        for(var i=0;i<temp.length;i++){
          var item = temp[i];
          if(id == item.id){
            if(1 == item.isDianZan){
              item.dianzan_num=--item.dianzan_num;
              item.isDianZan=0;
            }else if(0 == item.isDianZan){
              item.dianzan_num=++item.dianzan_num;
              item.isDianZan=1;
            }
          }
        }
        that.setData({
          xiewen:temp
        })
      break;
    }
  },
  pingluninput:function(e){
    var that = this;
    this.setData({
      pingLunContent:e.detail.value
    })
  },
  fasong:function(e){
      this.setData({
          "pingLunData.yifa":1
      })
    getApp().globalData.iswangchengpinglun=0;
    var that = this;
    var formdata = {
      pingLunContent:this.data.pingLunContent,
      uid:getApp().globalData.userInfo.uid,
      otherid:this.data.id,
      from:this.data.from
    }

      var i = setInterval(function(e){
          // console.log(getApp().globalData.iswangchengpinglun);
          if(1 == getApp().globalData.iswangchengpinglun){
              that.hidePingLun();
              that.getDataList();
              clearInterval(i);
          }
      },1000)
    // console.log(JSON.stringify(formdata))
    app.pinglun(formdata);

  },
    getDataList:function(){
        var that = this;
        wx.hideLoading();
        wx.request({
            url: app.data.server+'/index.php/Tongcheng/Book/getSheQuShouYeData?uid='+getApp().globalData.userInfo.uid,
            success:function(d){
                // console.log('123'+JSON.stringify(d.data));
                if(0 == d.data){
                    return;
                }
                that.setData({
                    hot:d.data.hot,
                    tuijian1:d.data.tuijian1,
                    tuijian2:d.data.tuijian2,
                    wenda:d.data.wenda,
                    xiewen:d.data.xiewen,
                })
                wx.stopPullDownRefresh();
            }
        })
    },
    bindinput:function(e){
      this.setData({
          keyword:e.detail.value
      })
    },
    bindconfirm:function(e){
    var that = this;
        if(e.detail.value.length <= 0){
            wx.showToast({
                title:'请填写要搜索的宝贝',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        this.setData({
            keyword:e.detail.value
        })
        wx.navigateTo({
          url: '/pages/community/searchList?keyword='+that.data.keyword
        })
        // this.getDataList();
    },
    search:function(e){
    var that = this;
      if(this.data.keyword.length <= 0){
          wx.showToast({
              title:'请填写要搜索的宝贝',
              icon: 'none',
              duration: 2000
          })
          return;
      }
        wx.navigateTo({
            url: '/pages/community/searchList?keyword='+that.data.keyword
        })
      // this.getDataList();
    },
    onShareAppMessage: function (res) {
        // if (res.from === 'button') {
        //   // 来自页面内转发按钮
        //   console.log(res.target)
        // }
        var id = res.target.dataset.id;
        var from = res.target.dataset.from;
        var path = '';
        if(3 == from){
          path = '/pages/community/shuping/info?isShare=1&id='+id;
        }else if(5 == from){
            path = '/pages/community/wenda/info?isShare=1&id='+id;
        }else if(6 == from){
          path = '/pages/community/xiewen/info?isShare=1&id='+id;
        }
        return {
            title: '分享',
            path: path
        }
    },
    getLunBoList:function(e){
        var that = this;
        wx.request({
            url: app.data.server+'/index.php/Tongcheng/Book/getLunboList?type=1',
            success:function(d){
                // console.log('123'+JSON.stringify(d.data));
                that.setData({
                    lunbotu:d.data
                })
            }
        })
    },
    bindfocus:function(e){
        var that = this;
        console.log('bindfocus')
        that.setData({
            "pingLunData.isshuru":1
        })
    },
    bindblur:function(e){
        var that = this;
        console.log('bindfocus')
        that.setData({
            "pingLunData.isshuru":0
        })
    },
    clearKeyWord:function(e){
      var that = this;
        console.log('clearKeyWord')
        this.setData({
            keyword:''
        })
        setTimeout(function(){
            that.getDataList()
        },1000)
    }
})